package com.student_exam_system.dao;

import com.student_exam_system.pojo.Question;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface QuestionMapper {
    /**添加一个Question
     * @param question 传入question类
     * @return int
     */
    int addQuestion(Question question);
    /**根据id删除一个Question
     * @param id 传入id
     * @return int
     */
    int deleteQuestionById(int id);

    /**更新Question
     * @param question 传入question类
     * @return int
     */
    int updateQuestion(Question question);
    /**根据id查询,返回一个Question
     * @param id 传入id
     * @return int
     */
    Question queryQuestionById(int id);
    /**查询全部Question,返回list集合
     * @return List<Question>
     */
    List<Question> queryAllQuestion();

    List<Question> queryQuestionsByPaperId(int paperId);
    /**
     * 添加试题【到试题库中】
     * @param question  试题对象
     * @return  1：添加成功 0：失败
     */
    @Insert("insert into exam.question values (0,#{question.getQuestionContent()},#{question.getQuestionType()}," +
            "#{question.getQuestionAnswer()},#{question.getQuestionScore()})")
    int insertQuestion(Question question);



    /**
     * 显示所有的试题
     * @return  question对象
     */
    @Select("select *from exam.question")
    @Results({
            @Result(column = "question_id" ,property = "questionId"),
            @Result(column = "question_content" ,property = "questionContent"),
            @Result(column = "question_type" ,property = "questionType"),
            @Result(column = "question_answer" ,property = "questionAnswer"),
            @Result(column = "question_score" ,property = "questionScore")
    })
    List<Question> showAllQuestions();



    /**
     * 往试卷中添加试卷
     * @param paperId  试卷id
     * @param questionId  试题id
     * @return  1：添加成功   0：添加失败
     */
    @Insert("insert into exam.paper_question values (0,#{questionId},#{paperId},0)")
    int addQtoP(@Param("paperId") int paperId,@Param("questionId") int questionId);

    /**
     * 取消往试卷中添加的试题
     * @param paperId  试卷低
     * @param questionId  试题id
     * @return 1：删除成功  0：删除失败
     */
    @Delete("delete from exam.paper_question where pq_question = #{questionId} and pq_paper = #{paperId}")
    int deleteQtoP(@Param("paperId")int paperId,@Param("questionId")int questionId);


    /**
     * 数据库总共有多少道题
     * @return int number
     */
    @Select("select count(*) from exam.question ")
    long sumQuestions();

    /**
     * 随机获取questionId
     * @param number  需要的个数
     * @return  list集合的questionId
     */
    @Select("SELECT * FROM user ORDER BY rand() LIMIT #{number}  FOR UPDATE")
    List<Integer> questionIds(int number);

}
